﻿using System.Data;
using System.Data.SqlClient;
using ContractManagermentSystem.Common;
using ContractManagermentSystem.Entity;

namespace ContractManagermentSystem.DataAccess
{
    public class ContractTypeDao
    {
        private readonly ConnectionHelper _connectionHelper;

        public ContractTypeDao()
        {
            _connectionHelper = new ConnectionHelper();
        }

        public DataTable LoadAllContractType()
        {
            return _connectionHelper.ExecuteDataTable("sp_GelAllContractType",null);
        }

        public DataTable GetContractTypeById(ContractType contractType)
        {
            var parameter = new SqlParameter("@ContractTypeId", SqlDbType.UniqueIdentifier) { Value = contractType.ContractTypeId };

            return _connectionHelper.ExecuteDataTable("sp_GetContractTypeById", parameter);
        }

        public void CreateContractType(ContractType contractType)
        {
            var parameters = new SqlParameter[2];

            parameters[0] = new SqlParameter("@ContractTypeName", SqlDbType.NVarChar, 30) {Value = contractType.ContractTypeName};
            parameters[1] = new SqlParameter("@IsActive", SqlDbType.Bit){Value = contractType.IsActive};

            _connectionHelper.ExecuteNonQuery("sp_CreateContractType", parameters);
        }

        public int CheckContractTypeName(string contractTypeName)
        {
            var parameter = new SqlParameter("@ContractTypeName", SqlDbType.NVarChar, 30) { Value = contractTypeName };

            return _connectionHelper.ExecuteScalar("sp_CheckContractTypeName", parameter);
        }

        public void SetIsActiveContractType(ContractType contractType)
        {
            var parameters = new SqlParameter[2];

            parameters[0] = new SqlParameter("@ContractTypeId",SqlDbType.UniqueIdentifier){Value = contractType.ContractTypeId};
            parameters[1] = new SqlParameter("@IsActive",SqlDbType.Bit){Value = contractType.IsActive};

            _connectionHelper.ExecuteNonQuery("sp_SetIsActiveContractType", parameters);
        }

        public void UpdateContractType(ContractType contractType)
        {
            var parameters = new SqlParameter[3];

            parameters[0] = new SqlParameter("@ContractTypeId", SqlDbType.UniqueIdentifier) { Value = contractType.ContractTypeId };
            parameters[1] = new SqlParameter("@ContractTypeName", SqlDbType.NVarChar, 30) { Value = contractType.ContractTypeName };
            parameters[2] = new SqlParameter("@IsActive", SqlDbType.Bit) { Value = contractType.IsActive };

            _connectionHelper.ExecuteNonQuery("sp_UpdateContractType", parameters);
        }

        public void DeleteContractType(ContractType contractType)
        {
            var parameter = new SqlParameter("@ContractTypeId", SqlDbType.UniqueIdentifier) { Value = contractType.ContractTypeId };

            _connectionHelper.ExecuteNonQuery("sp_DeleteContractType", parameter);
        }
    }
}
